<?php

/**
 * post处理模块
 */
class post
{
    public static function saveNew():bool{
        $name = argsTool::post("name");
        $time = argsTool::post("time");
        $type = argsTool::post("type");
        $content = argsTool::post("content");
        $xcid="A".substr(encryptTool::encode(time(),SECRET,true),0,15);
        $r = dbTool::insert("list",["xcid","name","time","type","content","checked"],[$xcid,$name,$time,$type,$content,"no"]);
        if ($r["status"]==="success"){
            echo "保存成功";
        }elseif ($r["status"]==="fail"){
            echo "保存失败";
        }
        return true;
    }

    public static function updateList():bool
    {
        $id = argsTool::post("id");
        $name = argsTool::post("name");
        $time = argsTool::post("time");
        $type = argsTool::post("type");
        $content = argsTool::post("content");
        $r = dbTool::select("list",["*"],["xcid"=>$id]);
        $checked = $r["data"][0]["checked"];
        $time = $time ?: $r["data"][0]["time"];
        $r = dbTool::update("list",["xcid","name","time","type","content","checked"],[$id,$name,$time,$type,$content,$checked],["xcid"=>$id]);
        if ($r["status"]==="success"){
            echo "保存成功";
        }elseif ($r["status"]==="fail"){
            echo "保存失败";
        }
        return true;
    }

    public static function getList():bool
    {
        $r = dbTool::select("list",["*"],["*"]);
        echo json_encode($r);
        return true;
    }

    public static function getListData():bool
    {
        $id = argsTool::post("id");
        $r = dbTool::select("list",["*"],["*"]);
        echo json_encode($r["data"][0]);
        return true;
    }

    public static function saveCheck():bool
    {
        $id = argsTool::post("id");
        $checked = argsTool::post("checked");
        $r = dbTool::select("list",["*"],["xcid"=>$id]);
        $d = $r["data"]["0"];
        $r = dbTool::update("list",["xcid","name","time","type","content","checked"],[$id,$d["name"],$d["time"],$d["type"],$d["content"],$checked],["xcid"=>$id]);
        return true;
    }

    public static function deleteList():bool
    {
        $id = argsTool::post("id");
        $r = dbTool::deleteRow("list",["xcid"=>$id]);
        if ($r["status"]==="success"){
            echo "删除成功";
        }elseif ($r["status"]==="fail"){
            echo "删除失败";
        }
        return true;
    }
}
